from django.db import models

from apps.common.school.models import Faculty
from utils.common import BaseModel
from apps.common.account.models import Account

STATUS_CHOICE = (('待审核', '待审核'), ('通过', '通过'), ('退回修改', '退回修改'))


# 获奖类别（A类，B类，...F类）
class PrizeCategory(BaseModel):
    name = models.CharField(verbose_name='类别', max_length=10)

    class Meta:
        db_table = "prize_system_prize_category"
        verbose_name_plural = verbose_name = "获奖类别管理"


# 获奖等级(全国赛，省级赛，校内赛）
class PrizeRank(BaseModel):
    name = models.CharField(verbose_name='等级名', max_length=10)

    class Meta:
        db_table = "prize_system_prize_rank"
        verbose_name_plural = verbose_name = "获奖等级"


# 获奖级别（一等奖，二等奖，...金奖，银奖，...）
class PrizeLevel(BaseModel):
    name = models.CharField(verbose_name='级别名', max_length=10)

    class Meta:
        db_table = "prize_system_prize_level"
        verbose_name_plural = verbose_name = "获奖级别"


# 获奖
class Prize(BaseModel):
    title = models.CharField(max_length=400, verbose_name='奖励名称')
    results_name = models.CharField(max_length=100, verbose_name='成果名称')
    scientific_prize_type = models.CharField(verbose_name='奖励(科研奖励用)类别', max_length=50, default='')
    people_cnt = models.PositiveSmallIntegerField(verbose_name='获奖人数', default=0)
    licence_issuing_authority = models.CharField(verbose_name='发证机关', max_length=50)
    prize_date = models.DateField(verbose_name='获奖日期')
    prize_level = models.ForeignKey(PrizeLevel, verbose_name='获奖级别', on_delete=models.CASCADE)
    prize_rank = models.ForeignKey(PrizeRank, verbose_name='获奖等级', on_delete=models.CASCADE)
    prize_category = models.ForeignKey(PrizeCategory, verbose_name='获奖等级', on_delete=models.CASCADE)
    complete_unit = models.CharField(verbose_name='完成单位', max_length=50)
    sum_unit_cnt = models.PositiveSmallIntegerField(verbose_name='总参加单位数', default=0)
    unit_rank = models.CharField(verbose_name='单位排名', max_length=10)
    authorization_num = models.CharField(verbose_name='奖励批准号', max_length=50)
    subject_type = models.CharField(verbose_name='学科门类', max_length=10)
    first_level_discipline = models.CharField(verbose_name='一级学科', max_length=50)
    project_source = models.CharField(verbose_name='项目来源', max_length=50)
    results_form = models.CharField(verbose_name='成果形式', max_length=20)
    prize_type = models.CharField(verbose_name='奖励类别', max_length=50, default='')
    remarks = models.TextField(verbose_name='备注', default='')

    first_check_status = models.CharField(verbose_name='一级审核状态', default='待审核', max_length=10, choices=STATUS_CHOICE)
    second_check_status = models.CharField(verbose_name='二级审核状态', default='待审核', max_length=10, choices=STATUS_CHOICE)

    class Meta:
        verbose_name_plural = verbose_name = '获奖'
        db_table = 'prize_system_prize'


# 获奖人信息
class PrizeAuthor(BaseModel):
    prize = models.ForeignKey(Prize, verbose_name='获奖', on_delete=models.CASCADE, related_name='author')
    sign_order = models.IntegerField(verbose_name='署名顺序', default=1)
    author_type = models.CharField(verbose_name='作者类型', max_length=10, default='本校老师')
    name = models.CharField(verbose_name='作者姓名', max_length=50, blank=False)
    job_num = models.CharField(verbose_name='作者职工号', max_length=50, default='')
    sex = models.CharField(verbose_name='性别', max_length=10, default='男')
    education = models.CharField(verbose_name='学历', max_length=50, default='其它')
    job_title = models.CharField(verbose_name='职称', max_length=50, default='')
    faculty_code = models.CharField(verbose_name='院系代码', max_length=30, default='', null=True, blank=True)
    faculty_title = models.CharField(verbose_name='院系名', max_length=50, default='', null=True, blank=True)
    account = models.ForeignKey(Account, verbose_name='本校作者', on_delete=models.SET_NULL, null=True)
    contribution_rate = models.PositiveSmallIntegerField(verbose_name='贡献率')

    class Meta:
        verbose_name_plural = verbose_name = '获奖人信息'
        db_table = 'prize_system_prize_author'


# 获奖附件
class PrizeFile(models.Model):
    prize = models.ForeignKey(Prize, verbose_name='获奖', on_delete=models.CASCADE, related_name='files')
    file = models.FileField(upload_to='prize_system/prize/', verbose_name='附件')
    file_type = models.CharField(verbose_name='附件类型', default='image', max_length=10)
    file_name = models.CharField(verbose_name='文件名', default='', max_length=100)

    class Meta:
        verbose_name_plural = verbose_name = '获奖附件'
        db_table = 'prize_system_prize_file'


# 获奖审核
class PrizeCheck(BaseModel):
    prize = models.ForeignKey(Prize, verbose_name='获奖', on_delete=models.CASCADE, related_name='prize_check')
    check_status = models.CharField(verbose_name='审核状态', max_length=20, choices=STATUS_CHOICE)
    check_opinion = models.CharField(max_length=100, verbose_name='审核意见', default='')
    check_level = models.PositiveSmallIntegerField(verbose_name='审核级别', default=1)

    class Meta:
        verbose_name_plural = verbose_name = '获奖审核'
        db_table = 'prize_system_prize_check'